<?php

namespace App\Http\Controllers\Mobile;

use App\Model\Business_group;
use App\Model\Group_member;
use App\Model\Leave;
use App\Model\Move_task;
use App\Model\Role_user;
use App\Model\Sch_desc;
use App\Model\Scheduling;
use App\Model\Settlement;
use App\Model\Stop_settle;
use App\Model\Workflow;
use App\Model\Workflow_desc;
use App\Model\Workflow_group;
use App\Model\Workflow_jl;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Uuid;

//审批
class ApprovalController extends CommonController
{
    //
	protected $Move_task;
	protected $Business_group;
	protected $Group_member;
	protected $Workflow;
	protected $Workflow_group;
	protected $Workflow_desc;
	protected $Workflow_jl;
	protected $Role_user;
	public  static $uid='';
	protected $Settlement;
	protected $Leave;
	protected $Stop_settle;
	protected $Scheduling;
	protected $Sch_desc;
	public function __construct()
	{
		parent::__construct();
		$this->Move_task=new Move_task();
		$this->Business_group=new Business_group();
		$this->Group_member=new Group_member();
		$this->Workflow=new Workflow();
		$this->Workflow_group=new Workflow_group();
		$this->Workflow_desc=new Workflow_desc();
		$this->Workflow_jl=new Workflow_jl();
		$this->Role_user=new Role_user();
		$this->Settlement=new Settlement();
		$this->Scheduling=new Scheduling();
		$this->Leave=new Leave();
		$this->Stop_settle=new Stop_settle();
		$this->Sch_desc=new Sch_desc();
		self::$uid=Auth::user()->id;


	}




//	中止审批列表（已审批）
	public function leave_list(){
//		查询该用户的审批业务组
		$data=$this->Workflow_desc->Leave_lists(self::$uid);
		return $this->success($data);
	}
//	中止审批列表（待审批）
	public function leave_list_no(){
//		查询该用户的审批业务组
		$data=$this->Workflow_desc->Leave_lists_no(self::$uid);
		return $this->success($data);

	}
//	临时调动审批列表(已审批)
	public function move_list(){
//		查询该用户的审批业务组
		$data=$this->Workflow_desc->move_lists(self::$uid);
		return $this->success($data);
	}
//	临时调动审批列表(待审批)
	public function move_list_no(){
//		查询该用户的审批业务组
		$data=$this->Workflow_desc->move_lists_no(self::$uid);
		return $this->success($data);
	}
//	终止结算审批列表（已审批）
	public function stop_list(Request $request){
//		查询该用户的审批业务组
		$data=$this->Workflow_desc->stop_list(self::$uid);
		$newArr=[];
		foreach($data as $kk=>$vv){
			$staff_task_id=$vv['stop_settle']['staff_task_id'];
//			已签到天数
			$data['sign_day']=count($this->Sch_desc->sign_staff_task($staff_task_id));
//			排班天数
			$data['sch_day']=count($this->Sch_desc->sch_day($staff_task_id));
//			中止时长
			$newArr[$kk]['id']=$vv['id'];
			$newArr[$kk]['status']=$vv['status']==2?'已同意':'不通过';
			$newArr[$kk]['realname']=$vv['userinfo']['realname'];
			$newArr[$kk]['avator']=$vv['userinfo']['avator'];
			$newArr[$kk]['station_name']=$vv['stop_settle']['staff_task']['store_station']['name'];
			$newArr[$kk]['phone']=$vv['userinfo']['phone'];
			$newArr[$kk]['avator']=$vv['userinfo']['avator'];
			$newArr[$kk]['sign_day']=$data['sign_day'].'/'.$data['sch_day'];
			$newArr[$kk]['leave_day']=$this->Leave->where(['staff_task_id'=>$staff_task_id])->sum('time');

		}
		$perPage = config('app.page');
		$total = count($newArr);
		if ($request->has('page')) {
			$current_page = $request->input('page');
			$current_page = $current_page <= 0 ? 1 :$current_page;
		} else {
			$current_page = 1;
		}
		$paginator =new LengthAwarePaginator($newArr, $total, $perPage, $current_page, [
			'path' => Paginator::resolveCurrentPath(),
			'pageName' => 'page',
		]);
		return $this->success($paginator);
	}
//	终止结算审批列表（待审批）
	public function stop_list_no(Request $request){
//		查询该用户的审批业务组
		$data=$this->Workflow_desc->stop_list_no(self::$uid);
		$newArr=[];
		foreach($data as $kk=>$vv){
			$staff_task_id=$vv['stop_settle']['staff_task_id'];
//			已签到天数
			$data['sign_day']=count($this->Sch_desc->sign_staff_task($staff_task_id));
//			排班天数
			$data['sch_day']=count($this->Sch_desc->sch_day($staff_task_id));
//			中止时长
			$newArr[$kk]['id']=$vv['id'];
			$newArr[$kk]['realname']=$vv['userinfo']['realname'];
			$newArr[$kk]['avator']=$vv['userinfo']['avator'];
			$newArr[$kk]['station_name']=$vv['stop_settle']['staff_task']['store_station']['name'];
			$newArr[$kk]['phone']=$vv['userinfo']['phone'];
			$newArr[$kk]['avator']=$vv['userinfo']['avator'];
			$newArr[$kk]['sign_day']=$data['sign_day'].'/'.$data['sch_day'];
			$newArr[$kk]['leave_day']=$this->Leave->where(['staff_task_id'=>$staff_task_id])->sum('time');

		}
		$perPage = config('app.page');
		$total = count($newArr);
		if ($request->has('page')) {
			$current_page = $request->input('page');
			$current_page = $current_page <= 0 ? 1 :$current_page;
		} else {
			$current_page = 1;
		}
		$paginator =new LengthAwarePaginator($newArr, $total, $perPage, $current_page, [
			'path' => Paginator::resolveCurrentPath(),
			'pageName' => 'page',
		]);

		return $this->success($paginator);
	}


	
//	签到申诉(已审批)
	public function complaint(Request $request){
		$where['time']=isset($request->time)?$request->time:date('Y-m',time());
		return 1;
	}
	//	签到申诉(待审批)
	public function complaint_no(Request $request){
		$where['time']=isset($request->time)?$request->time:date('Y-m',time());
		return 1;
	}






	public function is_approval($uid,$approval_id,$status,$reason){
		DB::beginTransaction();
		try{
//			查询申请审核记录表
			$descData=$this->Workflow_desc->descData($approval_id);
//			添加审核记录
			$jlData['workflow_id']=$approval_id;
			$jlData['approver_uid']=$uid;
			$jlData['status']=$status;
			$jlData['reason']=$reason;
			$id=$this->approve_id($descData['type'],$descData);
			$this->Workflow_jl->create($jlData);

			//清除其他角色的申请审批
			$this->Workflow_desc->where(['code'=>$descData['code']])
				->where('id','!=',$approval_id)->delete();
			$this->Workflow_desc->where('id','=',$approval_id)->update(['status'=>$status]);


//			有审批人拒绝
			if($status==3 || $descData['step']==4){
				$result=$this->update_approve($descData['type'],$id,$status);
			}else{
//				return $descData;
				//添加申请审核记录
				$workflowData=$this->Workflow->shengqin_jl($descData);


				if(!count($workflowData)){
//				没有下级审批人就不添加申请审核记录
					$result=$this->update_approve($descData['type'],$id,$status);
				}else{

					if($descData['step']==1){
						$desc['lovel_role']=$workflowData['two'];
					}elseif($descData['step']==2){
						$desc['lovel_role']=$workflowData['three'];
					}elseif($descData['step']==3){
						$desc['lovel_role']=$workflowData['four'];
					}
					$lovel_uid=$this->uids($desc['lovel_role']);
					$code=Uuid::uuid4();
					foreach($lovel_uid as $k=>$v){
						$arr[$k]['workflow_id']=$descData['workflow_id'];
						$arr[$k]['from_uid']=$descData['from_uid'];
						$arr[$k]['from_sid']=$descData['from_sid'];
						$arr[$k]['step']=$descData['step']+1;
						$arr[$k]['lovel_uid']=$v;
						$arr[$k]['lovel_role']=$desc['lovel_role'];
						$arr[$k]['status']=0;
						$arr[$k]['type']=$descData['type'];
						$arr[$k]['settlement_id']=$descData['settlement_id'];
						$arr[$k]['leave_id']=$descData['leave_id'];
						$arr[$k]['move_task_id']=$descData['move_task_id'];
						$arr[$k]['stop_id']=$descData['stop_id'];
						$arr[$k]['code']=$code;
					}
					$result=DB::table('workflow_desc')->insert($arr);
				}
			}
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}

		if(!isset($message)){
			return $this->stored($result);
		}else{
			return $this->failed($message);
		}


	}


//	审批
	public function is_agree(Request $request){
		$this->validate($request,[
			'approval_id'=>'required',
			'status'=>'required',
		]);
		$uid=self::$uid;
		$approval_id=$request->approval_id;
		$status=$request->status;
		$reason=isset($request->reason)?$request->reason:null;
		DB::beginTransaction();
		try{
			return $this->is_approval($uid,$approval_id,$status,$reason);
			DB::commit();
		}catch(\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->stored(['code'=>'200','message'=>'已审核']);
		}else{
			return $this->failed($exception);
		}



	}
















	public function uids($role_id){
		return $this->Role_user->where(['role_id'=>$role_id])->pluck('user_id');
	}



	//	查询审核id
	public function approve_id($type,$data){
		if($type==1){
			$id=$data['settlement_id'];
		}
		if($type==2){
			$id=$data['leave_id'];
		}
		if($type==3){
			$id=$data['move_task_id'];
		}
		if($type==4){
			$id=$data['stop_id'];
		}
		return $id;
	}

	public function update_approve($type,$id,$status)
	{
		$data['id']=$id;
		$data['status']=$status;
		if($type==1){
			 return $this->update_settle($data);
		}
		if($type==2){
			return $this->update_leave($data);
		}
		if($type==3){
			return $this->update_move($data);
		}
		if($type==4){
			return $this->update_stop($data);
		}
	}
	//	修改排班申请状态
	public function update_settle($data)
	{
		return $this->Scheduling->where(['id'=>$data['id']])->update(['is_agree'=>$data['status']]);
	}

	//	修改中止申请状态
	public function update_leave($data)
	{
		return $this->Leave->where(['id'=>$data['id']])->update(['status'=>$data['status']]);
	}


	//	修改调动申请状态
	public function update_move($data)
	{
		return $this->Move_task->where(['id'=>$data['id']])->update(['is_agree'=>$data['status']]);
	}

//		修改终止申请状态
	public function update_stop($data)
	{
//		return DB::table('stop_settle')->where(['id'=>$data['id']])->update(['is_agree'=>$data['status']]);
		return $this->Stop_settle->where(['id'=>$data['id']])->update(['is_agree'=>$data['status']]);
	}
}
